<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Shop extends AdminController{
	public $_validation = null;

	function __construct(){
		parent::__construct();
		$this->load->model('MShop');
		$this->load->model('MAdmin');
        //所有销售项目组
        $this->load->model('MAdminGroup');
		$this->_validation = Array(
			array('field'=>'name', 'label'=>'账户名称', 'rules'=>'trim|required|max_length[64]|callback__checkName')
			,array('field'=>'description', 'label'=>'账户描述', 'rules'=>'max_length[255]')
			,array('field'=>'owner_id', 'label'=>'所有者', 'rules'=>'required|integer|max_length[11]')
			,array('field'=>'status', 'label'=>'状态', 'rules'=>'integer')
			,array('field'=>'update_time', 'label'=>'更新时间', 'rules'=>'')
            ,array('field'=>'country', 'label'=>'国家', 'rules'=>'required')
		);

        //所有账户管理员,包含组长和组员的账号
        $allShopAdmins = $this->MAdmin->getAllShopAdmin();
        $this->assign('allShopAdmins', $allShopAdmins);
	}

	function _checkName($name){
	    $where = Array('name'=>$name);
	    if(isset($this->editObj) && $this->editObj){
	        $where['id <>'] = $this->editObj->id;
	    }
	    if($this->MShop->getAll($where)){
	        $this->form_validation->set_message('_checkName', '已经存在相同名称的账户！');
	        return false;
	    }
	    return true;
	}

	function index(){
		//$this->output->enable_profiler(TRUE);
		$this->load->library('FormFilter');
		$this->load->helper('formfilter');
		$this->formfilter->addFilter('name', 'like');
		$this->formfilter->addFilter('group_id','where');
		$this->formfilter->addFilter('status','where');
        $this->formfilter->addFilter('country','where');
		$limit = $this->pagination($this->MShop->getCount());
		$list  = $this->MShop->getList($limit);
        $group = $this->MAdminGroup->getAll();

        $number_perpage = filterValue('shop_number_perpage');
        $total_number = $this->MShop->getCount();
        $limit = $this->pagination($total_number,'',$number_perpage);
        $list = $this->MShop->getList($limit);
        $this->assign('total_number', $total_number);
        $this->assign('groups', object_column($group,'name','id'));
		$this->assign('list', $list);
		$this->layout();
	}

	function add(){
	    $obj = $this->MShop->createVo();
	    $this->load->library('form_validation');
	    $this->form_validation->set_rules($this->_validation);
	    if($this->form_validation->run()==TRUE){
	        $this->load->helper('populate');
	        $newObj = populate((Object)((Array)$obj), $this->form_validation->post());
	        //根据owner_id查对应的group_id
            $ownerInfo = $this->MAdmin->getOne(array('id'=>$newObj->owner_id));
	        $newObj->group_id =$ownerInfo->group_id;
	        if($this->MShop->add($newObj)){
	            successAndRedirect(l('edit_success'));
	        }else{
	            errorAndRedirect(l('data_fail'));
	        }
	    }

	    $this->assign('obj', $obj);
	    $this->layout();
	}

	function edit($id){
	    if(!is_numeric($id)){
	        show_error(l('id_not_null'));
	    }
	    $obj = $this->MShop->getOne(Array('id'=>$id));

	    if(!$obj){
	        errorAndRedirect('未找到定义！');
	    }
	    $this->editObj = $obj;
	    $this->load->library('form_validation');
	    $this->form_validation->set_rules($this->_validation);
	    if($this->form_validation->run()==TRUE){
	        $this->load->helper('populate');
	        $newObj = populate((Object)((Array)$obj), $this->form_validation->post());
            //根据owner_id查对应的group_id
            $ownerInfo = $this->MAdmin->getOne(array('id'=>$newObj->owner_id));
            $newObj->group_id =$ownerInfo->group_id;
	        if($this->MShop->update($newObj)){
	            successAndRedirect(l('edit_success'));
	        }else{
	            errorAndRedirect(l('data_fail'));
	        }
	    }
	    $this->assign('obj', $obj);
	    $this->layout();
	}
	function delete($id, $update_time){
	    if(!is_numeric($id) || !is_numeric($update_time)){
	        show_error(l('id_not_null'));
	    }
	    if(!$this->MShop->delete($id, $update_time)){
	        errorAndRedirect(l('data_fail'));
	    }
	    successAndRedirect(l('delete_success'));
	}

    function getShopsByGroupId(){
        $group_id = $this->input->post('group_id');
        if (!$group_id){
            exit(json_encode([
                'status' => 0,
                'msg'    => '请选择所属组'
            ]));
        }
        $shops = $this->MShop->getShopsByGroupId($group_id);
        exit(json_encode([
            'status' => 0,
            'msg'    => '获取成功',
            'data'   => $shops
        ]));
    }
}
